package com.example.administrator.yanfoxconn.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.activity.CarListActivity;
import com.example.administrator.yanfoxconn.activity.CyCarListActivity;
import com.example.administrator.yanfoxconn.activity.CyCarListActivityNEW;
import com.example.administrator.yanfoxconn.activity.CyDelListActivity;
import com.example.administrator.yanfoxconn.activity.JMSearchCar;
import com.example.administrator.yanfoxconn.activity.KMDmtListActivity;
import com.example.administrator.yanfoxconn.activity.SchoolServiceListActivity;
import com.example.administrator.yanfoxconn.bean.CarListMessage;
import com.example.administrator.yanfoxconn.bean.CyCarMessage;
import com.example.administrator.yanfoxconn.bean.JIMessage;
import com.example.administrator.yanfoxconn.bean.JVSchoolInfo;
import com.example.administrator.yanfoxconn.bean.KMDMT;

import java.util.List;

import static com.example.administrator.yanfoxconn.R.id.listMode;
import static com.example.administrator.yanfoxconn.R.id.tv_state;
import static com.example.administrator.yanfoxconn.R.id.tv_time;


/**
 * 碼頭貨物放行 車輛列表
 * 兼 物流消殺cy 車輛列表
 * Created by song on 2018/5/25.
 */

public class CarListAdapter extends BaseAdapter{

    private CarListActivity activity;
    private List<CarListMessage> listMessages;
    private String status;//狀態
    private String role = "";//角色
    private String dmtFlag = "";//多媒體 權限out取用 booked已預約
    private CyCarListActivity cyActivity;//物流消殺車輛界面
    private CyDelListActivity cyDelListActivity;//物流已消殺車輛界面
    private CyCarListActivityNEW cyCarListActivityNEW;//
    private SchoolServiceListActivity schoolServiceListActivity;
    private KMDmtListActivity kmDmtListActivity;
    private JMSearchCar jmSearchCar;
    private List<CyCarMessage> cyCarMessages;
    private List<JVSchoolInfo> jvSchoolInfoList;
    private List<JIMessage> jiMessages;
    private List<KMDMT> kmdmts;

    private OnClickListenerGoOrAdd onClickListenerGoOrAdd;
    private OnClickListenerCancel onClickListenerC;

    public CarListAdapter(CarListActivity activity, List<CarListMessage> listMessages){
        this.activity = activity;
        this.listMessages = listMessages;
    }

    public CarListAdapter(Context activity, List<CyCarMessage> cyCarMessages, String role) {
        if (role.equals("cy")) {
            this.cyActivity = (CyCarListActivity) activity;
        } else if(role.equals("two")||role.equals("ls")){
            this.cyCarListActivityNEW =(CyCarListActivityNEW) activity;
        }else {
            this.cyDelListActivity =(CyDelListActivity) activity;
        }
        this.cyCarMessages = cyCarMessages;
        this.role = role;
    }
    public CarListAdapter(SchoolServiceListActivity activity, List<JVSchoolInfo> jvSchoolInfos, String role) {
        this.schoolServiceListActivity = activity;
        this.jvSchoolInfoList = jvSchoolInfos;
        this.role = role;
    }
    public CarListAdapter(JMSearchCar activity, List<JIMessage> jiMessages, String role) {
        this.jmSearchCar = activity;
        this.jiMessages = jiMessages;
        this.role = role;
    }
    public CarListAdapter(KMDmtListActivity activity, List<KMDMT> kmdmts, String role,String dmtFlag) {
        this.kmDmtListActivity = activity;
        this.kmdmts = kmdmts;
        this.role = role;
        this.dmtFlag = dmtFlag;
    }

    @Override
    public int getCount() {
        if (role.equals("")) {
            return listMessages.size();
        } else  if (role.equals("school")) {
            return jvSchoolInfoList.size();
        } else if (role.equals("jmSearch")){
            return jiMessages.size();
        } else if (role.equals("dmt")){
            return kmdmts.size();
        }else{
            return cyCarMessages.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (role.equals("")) {
            return listMessages.get(position);
        }else  if (role.equals("school")) {
            return jvSchoolInfoList.get(position);
        }if (role.equals("jmSearch")){
            return jiMessages.get(position);
        } else if (role.equals("dmt")){
            return kmdmts.get(position);
        } else {
            return cyCarMessages.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("ResourceAsColor")
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            Log.e("===", "ss");
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_carlist_item, null);
            if (role.equals("school")){
                holder.sName = (TextView) convertView.findViewById(R.id.tv_car_id);
                holder.name = (TextView) convertView.findViewById(R.id.tv_dock);
                holder.sDate = (TextView) convertView.findViewById(R.id.tv_container);
                holder.eDate = convertView.findViewById(R.id.tv_ym);
                holder.tvGo = convertView.findViewById(R.id.tv_let_go);//放行
                holder.tvAdd = convertView.findViewById(R.id.tv_up_photo);//上傳
                holder.tvLook = convertView.findViewById(R.id.tv_look);//查看
            }else if (role.equals("jmSearch")){
                holder.tvCarnum =(TextView) convertView.findViewById(R.id.tv_car_id);
                holder.tvCartype =(TextView) convertView.findViewById(R.id.tv_dock);
                holder.tvQianfeng =(TextView) convertView.findViewById(R.id.tv_container);
                holder.tvCardoor =(TextView) convertView.findViewById(R.id.tv_jm_men);
                holder.tvQrdate =(TextView) convertView.findViewById(R.id.tv_jm_time);
                holder.llJm=convertView.findViewById(R.id.ll_jm);
                holder.tvJz=convertView.findViewById(R.id.tv_jz);
            }else if (role.equals("dmt")){
                holder.tvCancel = convertView.findViewById(R.id.tv_let_go);//放行
                holder.tvQrdate =(TextView) convertView.findViewById(R.id.tv_container);
                holder.tvCarnum =(TextView) convertView.findViewById(R.id.tv_car_id);
                holder.tvCartype =(TextView) convertView.findViewById(R.id.tv_dock);
            }else{
                holder.carId = (TextView) convertView.findViewById(R.id.tv_car_id);
                holder.dock = (TextView) convertView.findViewById(R.id.tv_dock);
                holder.container = (TextView) convertView.findViewById(R.id.tv_container);
                holder.state = (TextView) convertView.findViewById(R.id.tv_state);
                holder.tvGo = convertView.findViewById(R.id.tv_let_go);//放行
                holder.tvAdd = convertView.findViewById(R.id.tv_up_photo);//上傳
                holder.tvLook = convertView.findViewById(R.id.tv_look);//查看
                holder.time = convertView.findViewById(R.id.tv_time);//時間
                holder.ym = convertView.findViewById(R.id.tv_ym);
                holder.door = convertView.findViewById(R.id.tv_door);//門崗
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (role.equals("")) {
        holder.carId.setText(listMessages.get(position).getTruckno());
        holder.dock.setText(listMessages.get(position).getBuildingno());
        holder.container.setText(listMessages.get(position).getContainer());
        status = listMessages.get(position).getStatus();

        if (status.equals("1")) {
            holder.state.setText("準備裝貨");
            int color = activity.getResources().getColor(R.color.color_009adb);
            holder.state.setTextColor(color);
        }else if (status.equals("2")){
            holder.state.setText("正在裝貨");
            int color = activity.getResources().getColor(R.color.main_color);
            holder.state.setTextColor(color);
        }else if (status.equals("3")){
            holder.state.setText("裝貨完畢");
            int color = activity.getResources().getColor(R.color.color_fee123);
            holder.state.setTextColor(color);
        }else if (status.equals("4")){
            holder.state.setText("放行單列印完畢");
            int color = activity.getResources().getColor(R.color.viewfinder_mask);
            holder.state.setTextColor(color);
        }

        holder.tvGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenerGoOrAdd != null) {
                    onClickListenerGoOrAdd.OnClickListenerGo(position);
                }
            }
        });
            holder.tvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListenerGoOrAdd != null) {
                        onClickListenerGoOrAdd.OnClickListenerAdd(position);
                    }
                }
            });
            holder.tvLook.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListenerGoOrAdd != null) {
                        onClickListenerGoOrAdd.OnClickListenerLook(position);
                    }
                }
            });
        }else if (role.equals("school")){
            holder.tvGo.setText("確認");
            holder.tvAdd.setText("修改");
            holder.tvLook.setText("維護");

            holder.sName.setText(jvSchoolInfoList.get(position).getSCHOOL_NAME());
            holder.name.setText("窗口："+jvSchoolInfoList.get(position).getWINDOW_NAME());
            holder.sDate.setText("來："+jvSchoolInfoList.get(position).getP_DATE_COME());
            holder.eDate.setText("返："+jvSchoolInfoList.get(position).getP_DATE_RETURN());
            holder.sDate.setPadding(0,0,0,0);
            holder.eDate.setVisibility(View.VISIBLE);
            holder.tvGo.setBackgroundColor(schoolServiceListActivity.getResources().getColor(R.color.color_42D42B));
            holder.tvAdd.setBackgroundColor(schoolServiceListActivity.getResources().getColor(R.color.color_009adb));
            holder.tvLook.setBackgroundColor(schoolServiceListActivity.getResources().getColor(R.color.color_f5a306));
            holder.tvGo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListenerGoOrAdd != null) {
                        onClickListenerGoOrAdd.OnClickListenerGo(position);
                    }
                }
            });
            holder.tvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListenerGoOrAdd != null) {
                        onClickListenerGoOrAdd.OnClickListenerAdd(position);
                    }
                }
            });
            holder.tvLook.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListenerGoOrAdd != null) {
                        onClickListenerGoOrAdd.OnClickListenerLook(position);
                    }
                }
            });
        }else if (role.equals("jmSearch")){
            holder.tvCardoor.setVisibility(View.VISIBLE);
            holder.tvQrdate.setVisibility(View.GONE);
            holder.llJm.setVisibility(View.VISIBLE);
            holder.tvJz.setVisibility(View.VISIBLE);
            holder.tvCarnum.setText(jiMessages.get(position).getCarnum());
            //chang長度過長顯示不全所以換tv_jz顯示
            holder.tvCartype.setText("時間："+jiMessages.get(position).getQrdate());

            holder.tvJz.setText(jiMessages.get(position).getCartype()+"/"+jiMessages.get(position).getCardoor()+"門");
            holder.tvQianfeng.setText(jiMessages.get(position).getQianfeng());
            holder.tvQianfeng.setBackgroundColor(R.color.color_42D42B);
            holder.tvCardoor.setText(jiMessages.get(position).getHfd());
        }else if(role.equals("dmt")){

            holder.tvQrdate.setVisibility(View.VISIBLE);
            holder.tvCarnum.setText(kmdmts.get(position).getM_id());
            holder.tvCartype.setText(kmdmts.get(position).getM_kind()+kmdmts.get(position).getM_brand());
            if (dmtFlag.equals("booked")){
                holder.tvQrdate.setText("預約時間：" + kmdmts.get(position).getA_start_time()+"--"+kmdmts.get(position).getA_end_time());
            }else {
                holder.tvQrdate.setText("取用時間：" + kmdmts.get(position).getA_out_time());
            }

            holder.tvCancel.setText("取消");
            holder.tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListenerC != null) {
                        onClickListenerC.OnClickListenerC(position);
                    }
                }
            });
        }else{//物流防疫
            if (role.equals("two")) {
                holder.time.setVisibility(View.VISIBLE);
                holder.time.setText(cyCarMessages.get(position).getIN_DATE() + "--" + cyCarMessages.get(position).getOUT_DATE());
                if (!cyCarMessages.get(position).getFLAG().equals("")) {
                    Log.e("--------", "==cyCarMessages.get(position).getFLAG()==" + cyCarMessages.get(position).getFLAG() + "position==" + position);
//                    int color = activity.getResources().getColor(R.color.color_42D42B);
                    holder.time.setBackgroundResource(R.color.color_42D42B);
//                    holder.time.setBackgroundColor(Resources.getSystem().getColor(R.color.design_default_color_primary));
                } else {
                    holder.time.setBackgroundResource(R.color.color_eeeeee);
                }
                holder.door.setVisibility(View.VISIBLE);
                holder.door.setText(cyCarMessages.get(position).getDOOR());
            }
            if (role.equals("ls")) {
                holder.time.setVisibility(View.VISIBLE);
                holder.time.setText(cyCarMessages.get(position).getIN_DATE() + "--" + cyCarMessages.get(position).getOUT_DATE());

                holder.door.setVisibility(View.VISIBLE);
                holder.door.setText(cyCarMessages.get(position).getDOOR());
            }
//            else if(role.equals("cy")){
//                holder.time.setVisibility(View.VISIBLE);
//                holder.time.setText(cyCarMessages.get(position).getIN_DATE() + "--" + cyCarMessages.get(position).getOUT_DATE());
//            }
                int cyColor=0;
            if (role.equals("cy")) {
                if (cyCarMessages.get(position).getCODE_STATUSB().equals("綠碼")){
                     cyColor=cyActivity.getResources().getColor(R.color.color_42D42B);

                }else if(cyCarMessages.get(position).getCODE_STATUSB().equals("黃碼")){
                     cyColor=cyActivity.getResources().getColor(R.color.color_fee123);

                }
                if (cyCarMessages.get(position).getYMOK_FLAG().equals("Y")){
                    holder.ym.setVisibility(View.VISIBLE);
                    holder.ym.setText("可下車");
                    holder.ym.setBackgroundColor(cyActivity.getResources().getColor(R.color.color_42D42B));
                }else{
                    holder.ym.setVisibility(View.GONE);
                }
            } else if (role.equals("two")){
                if (cyCarMessages.get(position).getYMOK_FLAG().equals("Y")){
                    holder.ym.setVisibility(View.VISIBLE);
                    holder.ym.setText("可下車");
                    holder.ym.setBackgroundColor(cyCarListActivityNEW.getResources().getColor(R.color.color_42D42B));
                }else{
                    holder.ym.setVisibility(View.GONE);
                }

                if (cyCarMessages.get(position).getCODE_STATUSB().equals("綠碼")){
                    cyColor=cyCarListActivityNEW.getResources().getColor(R.color.color_42D42B);

                }else if(cyCarMessages.get(position).getCODE_STATUSB().equals("黃碼")){
                    cyColor=cyCarListActivityNEW.getResources().getColor(R.color.color_fee123);
                    holder.ym.setVisibility(View.GONE);
                }
            }else if (!role.equals("ls")){

                if (cyCarMessages.get(position).getYMOK_FLAG().equals("Y")){
                    holder.ym.setVisibility(View.VISIBLE);
                    holder.ym.setText("可下車");
                    holder.ym.setBackgroundColor(cyDelListActivity.getResources().getColor(R.color.color_42D42B));
                }else{
                    holder.ym.setVisibility(View.GONE);
                }
                if (cyCarMessages.get(position).getCODE_STATUSB().equals("綠碼")){
                    cyColor=cyDelListActivity.getResources().getColor(R.color.color_42D42B);

                }else if(cyCarMessages.get(position).getCODE_STATUSB().equals("黃碼")){
                    cyColor=cyDelListActivity.getResources().getColor(R.color.color_fee123);
                    holder.ym.setVisibility(View.GONE);
                }
            }
            holder.state.setBackgroundColor(cyColor);
            holder.carId.setText(cyCarMessages.get(position).getAPPLY_NO());

            holder.dock.setEllipsize(TextUtils.TruncateAt.END);
            holder.dock.setSingleLine(true);
            holder.dock.setText(cyCarMessages.get(position).getCOMPANY_NAME());
            holder.container.setText(cyCarMessages.get(position).getDRIVER_NAME());
            holder.state.setText(cyCarMessages.get(position).getCAR_NUM());

        }
        return convertView;
    }

    private class ViewHolder {
        public TextView carId,dock,container,state;//車牌號,碼頭,櫃號,狀態 或 單號,公司名稱,司機姓名,車牌號
        public TextView time,ym,door;//時間,疫苗,門崗
        public TextView name,sName,sDate,eDate;//接待人名稱，學校名稱，來煙日期，返回日期 人資大事記
        public TextView  tvGo, tvAdd,tvLook,tvCancel;//放行,提交,查看,dmt取消
        public TextView tvCarnum,tvCartype,tvCardoor,tvQianfeng,tvQrdate,tvJz;//车牌,车型,车门数,铅封,确认时间,集裝箱號
        public LinearLayout llJm;
    }

    public interface OnClickListenerGoOrAdd {
        void OnClickListenerGo(int position);
        void OnClickListenerAdd(int position);
        void OnClickListenerLook(int position);
    }

    public void setOnClickListenerGoOrAdd(OnClickListenerGoOrAdd onClickListenerGoOrAdd) {
        this.onClickListenerGoOrAdd = onClickListenerGoOrAdd;
    }
    public interface OnClickListenerCancel {
        void OnClickListenerC(int position);
    }

    public void setOnClickListenerCancel(OnClickListenerCancel onClickListenerc) {
        this.onClickListenerC = onClickListenerc;
    }
}
